﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

namespace Xceed.Silverlight.Data
{
  [DebuggerDisplay( "{DebuggerDisplayString}" )]
  internal sealed class ReactiveOperationInfo<T>
  {
    #region Constructor

    internal ReactiveOperationInfo(
      SourcePosition sourcePosition,
      long expectedMoveCount,
      int expectedFetchCount,
      bool approximateMove,
      DataSourceContext context )
    {
      if( context == null )
        throw new ArgumentNullException( "context" );

      m_parameters = new JumpMoveAndFetchParameters(
                       sourcePosition,
                       expectedMoveCount,
                       expectedFetchCount,
                       approximateMove,
                       context );

      this.ActualMoveCount = null;
      this.FetchedItems = null;
      this.Result = true;
    }

    #endregion

    #region SourcePosition Property

    internal SourcePosition SourcePosition
    {
      get
      {
        return m_parameters.SourcePosition;
      }
      set
      {
        m_parameters = m_parameters.CreateWithNewSourcePosition( value );
      }
    }

    #endregion

    #region ExpectedMoveCount Property

    internal long ExpectedMoveCount
    {
      get
      {
        return m_parameters.ExpectedMoveCount;
      }
      set
      {
        m_parameters = m_parameters.CreateWithNewMoveCount( value );
      }
    }

    #endregion

    #region ExpectedFetchCount Property

    internal int ExpectedFetchCount
    {
      get
      {
        return m_parameters.ExpectedFetchCount;
      }
      set
      {
        m_parameters = m_parameters.CreateWithNewFetchCount( value );
      }
    }

    #endregion

    #region ApproximateMove Property

    internal bool ApproximateMove
    {
      get
      {
        return m_parameters.ApproximateMove;
      }
      set
      {
        m_parameters = m_parameters.CreateWithNewApproximateMoving( value );
      }
    }

    #endregion

    #region Context Property

    internal DataSourceContext Context
    {
      get
      {
        return m_parameters.Context;
      }
    }

    #endregion

    #region DebuggerDisplayString Private Property

    private string DebuggerDisplayString
    {
      get
      {
        string moveMode = this.ApproximateMove ? "approximative" : "precise";
        string fetchedItems;

        if( this.FetchedItems != null )
        {
          StringBuilder sb = new StringBuilder();

          foreach( T item in this.FetchedItems )
          {
            if( sb.Length > 0 )
            {
              sb.Append( "," );
            }

            DataPath path = item as DataPath;
            sb.Append( path.LastChild.ToString() );
          }

          fetchedItems = sb.ToString();
        }
        else
        {
          fetchedItems = "null";
        }

        return string.Format( "Operation: Source={0}, Move={1} {5}, Fetch={2}, EndPosition={3}, FetchedItems=({4})", this.SourcePosition.ToString().PadRight( 70 ), this.ExpectedMoveCount, this.ExpectedFetchCount, this.EndPosition, fetchedItems, moveMode );
      }
    }

    #endregion

    internal JumpMoveAndFetchParameters GetParameters()
    {
      return m_parameters;
    }

    internal Nullable<long> ActualMoveCount;
    internal ICollection<T> FetchedItems;
    internal bool Result;
    internal SourcePosition EndPosition;
    internal RelativeOffset EndPositionOffset;
    internal EstimateInt64 TotalCount;

    #region Private Fields

    private JumpMoveAndFetchParameters m_parameters;

    #endregion
  }
}
